#[cfg(test)]
mod tests {
    use crate::handler::common::response::common::AppResult;
    use axum::{
        http::{StatusCode, header},
        response::IntoResponse,
    };
    use serde::Serialize;

    #[derive(Serialize)]
    struct TestData {
        id: u32,
        name: String,
    }

    #[tokio::test]
    async fn test_app_result_success_response() {
        let test_data = TestData {
            id: 1,
            name: "测试".to_string(),
        };

        let app_result = AppResult {
            data: Some(test_data),
            code: 200,
            message: Option::from("成功".to_string()),
        };

        let response = app_result.into_response();

        assert_eq!(response.status(), StatusCode::OK);

        let content_type = response.headers().get(header::CONTENT_TYPE);
        assert!(content_type.is_some());
        assert_eq!(content_type.unwrap(), "application/json");
    }

    #[tokio::test]
    async fn test_app_result_none_data_response() {
        let app_result: AppResult<TestData> = AppResult {
            data: None,
            code: 404,
            message: Option::from("未找到".to_string()),
        };

        let response = app_result.into_response();

        assert_eq!(response.status(), StatusCode::OK);

        let content_type = response.headers().get(header::CONTENT_TYPE);
        assert!(content_type.is_some());
        assert_eq!(content_type.unwrap(), "application/json");
    }

    #[test]
    fn test_app_result_serialization() {
        let test_data = TestData {
            id: 1,
            name: "测试".to_string(),
        };

        let app_result = AppResult {
            data: Some(test_data),
            code: 200,
            message: Option::from("成功".to_string()),
        };

        let json = serde_json::to_string(&app_result).unwrap();
        assert!(json.contains("\"code\":200"));
        assert!(json.contains("\"message\":\"成功\""));
        assert!(json.contains("\"data\""));
    }

    #[test]
    fn test_app_result_serialization_with_none_data() {
        let app_result: AppResult<TestData> = AppResult {
            data: None,
            code: 404,
            message: Option::from("未找到".to_string()),
        };

        let json = serde_json::to_string(&app_result).unwrap();
        assert!(json.contains("\"code\":404"));
        assert!(json.contains("\"message\":\"未找到\""));
        // 由于使用了 skip_serializing_if，None 的 data 字段不应该出现在 JSON 中
        assert!(!json.contains("\"data\""));
    }

    #[test]
    fn test_app_result_creation() {
        let app_result = AppResult {
            data: Some("测试数据"),
            code: 200,
            message: Option::from("操作成功".to_string()),
        };

        assert_eq!(app_result.code, 200);
        assert_eq!(app_result.message, Some("操作成功".to_string()));
        assert!(app_result.data.is_some());
        assert_eq!(app_result.data.unwrap(), "测试数据");
    }

    #[test]
    fn test_app_result_error_case() {
        let app_result: AppResult<String> = AppResult {
            data: None,
            code: 500,
            message: Option::from("内部服务器错误".to_string()),
        };

        assert_eq!(app_result.code, 500);
        assert_eq!(app_result.message, Some("内部服务器错误".to_string()));
        assert!(app_result.data.is_none());
    }
}
